package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * ClassName: EmployeeController
 * Package: com.itheima.controller
 * Description:
 *
 * @Author: @Albert
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService service;

    /**
     * 前端传过来的是json格式的参数,所以需要添加注解  @RequestBody
     * request参数,将查询的员工ID存入session
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        /*
         * 1.将页面提交的密码进行md5加密
         * 2.根据用户名查询数据库
         * 3.查询不到的话返回
         * 4.对比密码,失败则返回
         * 5.查看员工状态,禁用的话返回禁用结果
         * 6.成功,将员工ID存入session返回成功登录结果
         */
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = service.getOne(wrapper);
        if(emp==null){
            return R.error("查询不到登陆失败");
        }
        if(!emp.getPassword().equals(password)){
            return R.error("密码错误登陆失败");
        }
        if(emp.getStatus()==0){
            return R.error("账号被禁用");
        }
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("开始新增员工.员工信息为{}",employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());*/
        Long empId=(Long)request.getSession().getAttribute("employee");
        /*employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/
        service.save(employee);
        return R.success("新增员工成功");
    }

    @GetMapping("/page")
    public R<Page> getList(int page,int pageSize,String name){
        log.info("page:{},pageSize:{},name:{}",page,pageSize,name);
        Page pageInfo = new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper();
        //添加过滤条件
        wrapper.like(!StringUtils.isEmpty(name),Employee::getName,name);
        //添加排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);
        service.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> update(HttpServletRequest request ,@RequestBody Employee employee){
        Long id=(Long)request.getSession().getAttribute("employee");
        log.info("前端传过来的信息:{}",employee);
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(id);
        service.updateById(employee);
        return R.success("员工信息修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("根据ID查找员工信息");
        Employee employee = service.getById(id);
        return R.success(employee);
    }

}
